package com.example.springboot159.springboot.chapter03;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.NoOp;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy {
    @Override
    public Object instantiation(BeanDefinition definition, String beanName, Constructor ctor, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, Exception {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(definition.getBean());
        enhancer.setCallback(new NoOp() {
            @Override
            public int hashCode() {
                return super.hashCode();
            }
        });
        if (null == ctor)
            return enhancer.create();

        return enhancer.create(ctor.getParameterTypes(),args);
    }
}
